package com.fulihui.wgj.admin.api.wxtemplate;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateECRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateLogRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateCancelRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.SendTemplateDetailVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.SendTemplateLogVO;
import com.fulihui.wgj.admin.integration.SendTemplateLogServiceClient;
import com.fulihui.wgj.admin.integration.SendTemplateMsgServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-27 18:01
 */
@RestController
@RequestMapping("/template/ec")
@Api(tags = "电商模板消息发送")
public class SendEcTemplateController extends BaseController {

    @Autowired
    private SendTemplateMsgServiceClient sendTemplateMsgServiceClient;

    @Autowired
    private SendTemplateLogServiceClient sendTemplateLogServiceClient;

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ApiOperation("发送模板消息")
    public Result<Boolean> send(@RequestBody @Valid SendTemplateECRequest request, BindingResult validResult,
                                HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendTemplateMsgServiceClient.sendEcTemplate(request);
    }

    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation("计算人数")
    public Result<String> countSendUser(@RequestBody @Valid SendTemplateECRequest request, BindingResult validResult,
                                        HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendTemplateMsgServiceClient.countSendUser(request);
    }

    @RequestMapping(value = "/log", method = RequestMethod.POST)
    @ApiOperation("电商/游戏/本地模板发送记录分页查询")
    public Result<PageView<SendTemplateLogVO>> sendLogPage(@RequestBody @Valid SendTemplateLogRequest request, BindingResult validResult,
                                                           HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<SendTemplateLogVO> pageView = sendTemplateLogServiceClient.sendTemplateLog(request);
        return JsonResultBuilder.succ(pageView);
    }


    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation("电商/游戏 根据id查询发送详细")
    public Result<SendTemplateDetailVO> sendLogDetail(@RequestBody @Valid SendTemplateQueryRequest request, BindingResult validResult,
                                                      HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        Result<SendTemplateDetailVO> sendTemplateLogVOResult = sendTemplateLogServiceClient.queryLogById(request);
        return sendTemplateLogVOResult;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("电商编辑发送模板消息")
    public Result<Boolean> edit(@RequestBody @Valid SendTemplateECRequest request, BindingResult validResult,
                                HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendTemplateMsgServiceClient.editSendEcTemplate(request);
    }

    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ApiOperation("取消发送")
    public Result<Boolean> cancel(@RequestBody @Valid TemplateCancelRequest request, BindingResult validResult,
                                  HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendTemplateLogServiceClient.cancelSend(request);
    }
}
